var tools = require('./vueTools.js').default;
var common = new function() {
    this.rootVue = undefined;
    this._setVue = function(vue) {
        this.rootVue = vue;
    }
    this.post = function(url, data, sucfun, errorfuc) {
        this.http(url, data, sucfun, errorfuc, "post")
    };
    this.postNotLoading = function(url, data, sucfun, errorfuc) {
        this.http(url, data, sucfun, errorfuc, "post", false)
    };
    this.get = function(url, data, sucfun, errorfuc) {
        this.http(url, data, sucfun, errorfuc, "get")
    };
    this.renderUrl = function(url, data) {

    };
    this.getHttpHeaders = function() {
        return {};
    };
    this.xujiqi = function() {

    };
    this.http = function(url, data, sucfun, errorfuc, method, hasLoading) {
        if (this.xujiqi(url, data) == false) {
            return;
        }
        hasLoading = hasLoading == undefined ? true : hasLoading;
        var loading = undefined;
        if (hasLoading) {
            loading = this.rootVue.$loading({
                lock: true,
                text: '正在加载中',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
        }

        data = data == undefined ? {} : data;
        var params = {};
        for (var k in data) {
            var value = data[k] + "";
            if (url.indexOf("{" + k + "}") != -1) url = url.replace("{" + k + "}", value);
            //else params.append(k, value);
            params[k] = value;
        }
        var headers = this.getHttpHeaders();
        var rootVue = this.rootVue;
        tools.ezajax(url, params, function(res) {
            if (loading != undefined) {
                loading.close();
            }
            if (sucfun == undefined) sucfun = function() {};
            if (errorfuc == undefined) errorfuc = function(error) {
                rootVue.$message.error(error.resultMsg);
            };
            var object_type = typeof res;
            if ("object" == object_type.toLowerCase()) {
                if (res.resultCode == 200) sucfun(res);
                else if (res.resultCode == 500) {
                    errorfuc(res);
                } else if (res.resultCode == 301) {
                    window.location.href = res.resultMap;
                    return;
                } else if (res.resultCode == 401) {
                    rootVue.$cookies.remove('loginInfo');
                    rootVue.$cookies.remove('token');
                    if (res.ext == "1") {
                        window.location.href = "/company_login";
                    } else {
                        window.location.href = "/login";
                    }
                } else {
                    sucfun(res);
                }

            } else {
                sucfun(res);
            }

        }, function(res) {
            if (loading != undefined) {
                loading.close();
            }
            if (errorfuc != undefined) {
                errorfuc(res);
            }
        }, method, headers)
    };
    this.test = function() {
        alert("test");
    };
    this.toPage = function(path, forcefresh) {
        if (forcefresh == true) {
            window.location.href = path;
        } else {
            this.rootVue.$router.push({
                path: path
            });
        }
    };
    this.isEquall = function(datas) {
        var that = this;
        var equall = true;
        var arrs = [];
        datas.forEach(function(v, i) {
            arrs.push(v.certificateName);
        });
        var firstVal = arrs[0]; //取得数组第一项，然后和其他项对比
        var firstValLen = arrs[0].length;
        for (var i = 0; i < arrs.length; i++) {
            var lengths = arrs[i].length;
            if (arrs[i].substr(lengths - 3) != firstVal.substr(firstValLen - 3)) {
                // alert("不全等");
                equall = false;
                break;
            }
        }
        return equall;
    };
    this.isFirefox = function() {
        var a = false;
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        //判断是否Firefox浏览器
        if (userAgent.indexOf("Firefox") > -1) {
            a = true;
        }
        return a;
    };
    this.isChrome = function() {
        var a = false;
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        //判断是否Firefox浏览器
        if (userAgent.indexOf("Chrome") > -1) {
            a = true;
        }
        return a;
    };
    this.isMobile = function() {
        let flag = navigator.userAgent.match(
            /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
        )
        return flag;
    };
    this.getQuery = function(param) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == param) {
                return pair[1];
            }
        }
        return '';
    };
    return this;
};

export default common
// module.exports=common
